<?php
/**
 * @author Mazvv
 * @abstract
 * @package Extenders
 */
abstract class CRM_Model extends Model{
	protected $ciObject;
	protected $userRid;
	protected $nowTime;
	
	public function __construct() {
		parent::Model();
		$this->ciObject = &get_instance();
		$this->userRid = $this->ciObject->user->GetUserRid();
		$this->nowTime = date("Y-m-d H:i:s");
		$this->db->query('set names \'utf8\'');
	}
	
	
	public function GetTypeDoc(){
		if($controller = $this->ciObject->uri->segment('1')){
			$query = $this->db->getwhere('_documents_types', array('controller'=>$controller, 'archive'=>0));
			return ($query->num_rows())?$query->row():False;	
		}
		return False;
	}
	
	public function DBGet($p_fromTable){
		if($area = $this->ciObject->menucontainer->GetItemArea()){
			switch ($area){
				case 'FILIAL':{
					if($p_fromTable == '_users' || $p_fromTable == '_employeers' || $p_fromTable == '_filials') break;
					$this->db->join('_users', "{$p_fromTable}.modifier_users_rid = _users.rid and _users.archive = 0");
					$this->db->join('_employeers', "_users._employeers_rid = _employeers.rid and _employeers.archive = 0");
					$this->db->join('_filials', "_employeers._filials_rid = _filials.rid and _filials.archive = 0");
					$this->db->where(array('_filials.rid'=>$this->ciObject->user->GetFilialRid()));
					break;
				}
				case 'OWN':{
					if($p_fromTable == '_users') break;
					$this->db->join('_users', "{$p_fromTable}.modifier_users_rid = _users.rid and _users.archive = 0");
					$this->db->where(array('_users.rid'=>$this->ciObject->user->GetUserRid()));
					break;
				}
				default: {break;}
			}
		}
		return $this->db->get();
	}
	
	public function toMySQLDate($p_date){
		$tArr = explode('.', $p_date);
		return implode('-', array_reverse($tArr));
	}
}
?>